local sk__qinguo = fk.CreateSkill {

  name = "sk__qinguo",

  tags = {},

}


sk__qinguo:addEffect("active", {
  name = "sk__qinguo",
  anim_type = "support",
  card_num = 1,
  target_num = 1,
  prompt = "#sk__qinguo",
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip and
        Fk:currentRoom():getCardArea(to_select) ~= Card.PlayerEquip
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and #selected_cards == 1 and
        not player:isProhibited(to_select, Fk:getCardById(selected_cards[1]))
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:useCard({
      from = player,
      tos = { target },
      card = Fk:getCardById(effect.cards[1]),
    })
  end,
})

sk__qinguo:addEffect(fk.CardUseFinished, {
  name = "#sk__qinguo_trigger",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__qinguo.name) then
      return target == player and data.card.type == Card.TypeEquip
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("sk__qinguo_cards") == 0 then
      room:setPlayerMark(player, "sk__qinguo_cards", U.getUniversalCards(room, "b"))
    end
    local success, dat = player.room:askForUseActiveSkill(player, "sk__qinguo_viewas", "#sk__qinguo-use", true,
      { bypass_times = true })
    if success and dat then
      event:setCostData(self, dat)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = table.simpleClone(event:getCostData(self))
    local card = Fk:cloneCard(Fk:getCardById(dat.cards[1]).name)
    card.skillName = sk__qinguo.name
    room:useCard {
      from = player,
      tos = table.map(dat.targets, Util.Id2PlayerMapper),
      card = card,
      extraUse = true,
    }
  end,
})
sk__qinguo:addEffect(fk.AfterCardsMove, {
  name = "#sk__qinguo_trigger",
  mute = true,
  --sk__qinguo,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__qinguo.name) then
      if player:getMark("sk__qinguo-turn") == 0 then
        local cards = {}
        for _, move in ipairs(data) do
          if move.from and move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerEquip then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
        cards = player.room.logic:moveCardsHoldingAreaCheck(cards)
        if #cards > 0 then
          event:setCostData(self, cards)
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room

    local cards, choice = room:askToChooseCardsAndChoice(player,{
      cards=event:getCostData(self),
      choices={
        "OK"
      },
      skill_name=sk__qinguo.name,
      prompt="#sk__qinguo-prey",
      cancel_choices={"get_all", "Cancel"},
    })
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    elseif choice == "get_all" then
      event:setCostData(self, event:getCostData(self))
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      room:setPlayerMark(player, "sk__qinguo-turn", 1)
      room:moveCardTo(event:getCostData(self), Card.PlayerHand, player, fk.ReasonJustMove, "sk__qinguo", nil, true, player)
  end,
})

return sk__qinguo
